package command

import (
	"ssadmin/common/constant"
	"ssadmin/common/utils"
	"ssadmin/internal/admin/models"
	"strconv"
	"strings"
	"time"

	"gitee.com/sansaniot/ssiot-core/httpmvc/jwtauth/user"
	"github.com/gin-gonic/gin"
	uuid "github.com/satori/go.uuid"
)

type UserInsertReq struct {
	UserId     string `json:"-" comment:"用户id"` // 用户id
	Username   string `json:"username" comment:"用户名" vd:"len($)>0"`
	Password   string `json:"password" comment:"密码"`
	DeptId     string `json:"deptId" comment:"部门id"`
	Email      string `json:"email" comment:"邮箱"`
	Mobile     string `json:"mobile" comment:"联系电话"`
	Status     string `json:"status" comment:"状态 0锁定 1有效" vd:"len($)>0"`
	CreateTime time.Time
	ModifyTime time.Time
	//LastLoginTime time.Time `json:"lastLoginTime" comment:"最近访问时间"`
	Sex         string `json:"sex" comment:"性别 male 男 female 女 unknown 保密"`
	Description string `json:"description" comment:"描述"`
	Avatar      string `json:"avatar" comment:"用户头像"`
	CreatorId   string
	Creator     string
	ValidDate   time.Time `json:"validDate" comment:"过期时间"`
	Company     string    `json:"company" comment:""`
	Layout      string    `json:"layout" comment:""`
	Language    string    `json:"language" comment:"语言"`
	Timezone    string    `json:"timezone" comment:"时区"`
	Alias       string    `json:"alias" comment:""`
	Agency      string    `json:"agency" comment:""`
	IsAgency    string    `json:"isAgency" comment:""`
	OpenId      string    `json:"openId" comment:""`
	PositionKey string    `json:"positionKey" comment:""`
	DeptIds     string    `json:"deptIds"`
	RoleId      string    `json:"roleId"`
	Goto        string    `json:"goto"`
	TokenExp    int64     `json:"tokenExp"`
}

func (s *UserInsertReq) Bind(c *gin.Context) {
	//获取扩展属性（自定义扩展）
	s.Creator = user.Get(c, constant.CacheKeyUserName).(string)
	s.CreatorId = user.Get(c, constant.CacheKeyUserId).(string)
	s.CreateTime = time.Now()

}

func (s *UserInsertReq) Generate(model *models.User) {
	model.UserId = uuid.NewV4().String()
	model.Username = s.Username
	if len(s.Password) > 0 {
		model.Password = utils.NewUserDefaultPwd(s.Username, s.Password)
	} else {
		model.Password = utils.NewUserDefaultPwd(s.Username)
	}
	model.Email = s.Email
	model.Mobile = s.Mobile
	model.Status = s.Status
	model.CreateTimeDts = time.Now()
	model.ModifyTime = time.Now()
	//model.LastLoginTime = s.LastLoginTime
	model.Sex = s.Sex
	model.Description = s.Description
	model.Avatar = s.Avatar
	model.CreatorId = s.CreatorId
	model.Creator = s.Creator
	model.ValidDate = s.ValidDate
	model.Company = s.Company
	model.Language = s.Language
	model.Timezone = s.Timezone
	model.Alias = s.Alias
	model.Agency = s.Agency
	model.IsAgency = s.IsAgency
	model.OpenId = s.OpenId
	model.PositionKey = s.PositionKey
	model.RoleId = s.RoleId
	model.DeptIds = s.DeptIds
	model.Goto = s.Goto
	model.TokenExp = s.TokenExp
}

func (s *UserInsertReq) GetId() interface{} {
	return s.UserId
}

type UserUpdateReq struct {
	UserId      string    `json:"id" comment:"用户id" vd:"len($)>0"` // 用户id
	Email       string    `json:"email" comment:"邮箱"`
	Mobile      string    `json:"mobile" comment:"联系电话"`
	Status      string    `json:"status" comment:"状态 0锁定 1有效"`
	ModifyTime  time.Time `json:"modifyTime" comment:"修改时间"`
	Sex         string    `json:"sex" comment:"性别 male 男 female 女 unknown 保密"`
	Description string    `json:"description" comment:"描述"`
	Avatar      string    `json:"avatar" comment:"用户头像"`
	ValidDate   time.Time `json:"validDate" comment:"过期时间"`
	Company     string    `json:"company" comment:""`
	Layout      string    `json:"layout" comment:""`
	Language    string    `json:"language" comment:"语言"`
	Timezone    string    `json:"timezone" comment:"时区"`
	Alias       string    `json:"alias" comment:""`
	Agency      string    `json:"agency" comment:""`
	IsAgency    string    `json:"isAgency" comment:""`
	OpenId      string    `json:"openId" comment:""`
	PositionKey string    `json:"positionKey" comment:""`
	DeptIds     string    `json:"deptIds"`
	RoleId      string    `json:"roleId"`
	Goto        string    `json:"goto"`
	TokenExp    int64     `json:"tokenExp"`
	UserInfo    map[string]interface{}
}

func (s *UserUpdateReq) Generate(model *models.User) {
	if len(s.UserId) == 0 {
		model.UserId = strconv.FormatInt(time.Now().UnixNano()/1e6, 10)
	} else {
		model.UserId = s.UserId
	}

	model.Email = s.Email
	model.Mobile = s.Mobile
	model.Status = s.Status
	model.ModifyTime = time.Now()
	model.Sex = s.Sex
	model.Description = s.Description
	model.Avatar = s.Avatar
	model.ValidDate = s.ValidDate
	model.Company = s.Company
	model.Language = s.Language
	model.Timezone = s.Timezone
	model.Alias = s.Alias
	model.OpenId = s.OpenId
	model.PositionKey = s.PositionKey
	model.RoleId = s.RoleId
	model.DeptIds = s.DeptIds
	model.Goto = s.Goto
	model.TokenExp = s.TokenExp
}

func (s *UserUpdateReq) GetId() interface{} {
	return s.UserId
}

// UserDeleteReq 功能删除请求参数
type UserDeleteReq struct {
	Id            string `json:"id"`
	Ids           string `json:"ids"`
	CurrentUserId string
}

func (s *UserDeleteReq) GetId() []string {
	if len(s.Ids) > 0 {
		return strings.Split(s.Ids, ",")
	} else {
		return strings.Split(s.Id, ",")
	}
}

type PasswordUpdateReq struct {
	Username    string
	Password    string `json:"password" comment:"原始密码"`
	NewPassword string `json:"newPassword" comment:"密码"`
}

type PasswordResetReq struct {
	Username string `json:"username" comment:"用户名"`
	Password string `json:"password" comment:"密码"`
}

type QrCodeCacheReq struct {
	QrCode string `json:"qrCode" comment:"二维码"`
}

type UserRole struct {
	UserId string
	RoleId string
}
type UserDept struct {
	UserId string
	DeptId string
}

type IdentifyReq struct {
	Type      string `json:"type"`
	Key       string `json:"key"`
	Url       string `json:"url"`
	Username  string `json:"username"`
	Token     string `json:"token"`
	Timestamp string `json:"timestamp"`
}
